import {Player} from "db://assets/scripts/data/model/Player";
import {CSVPaths} from "db://assets/scripts/common/CSVPaths";
import {CSVManager} from "db://assets/scripts/common/CSVManager";
import {EventManager} from "db://assets/scripts/common/EventManager";
import {EventType} from "db://assets/scripts/common/EventType";
import {StorageManager} from "db://assets/scripts/common/StorageManager";
import {StorageKey} from "db://assets/scripts/common/StorageKey";
import {GameManager} from "db://assets/scripts/game/GameManager";
import {PlayerDataManager} from "db://assets/scripts/common/PlayerDataManager";

/**
 * 玩家技能管理器
 */
export class PlayerSkillManager {
    /**
     * 玩家
     */
    private player: Player;

    /**
     * 玩家技能管理器实例
     */
    private static INSTANCE: PlayerSkillManager;

    /**
     * 获取玩家技能管理器实例
     */
    public static getInstance(): PlayerSkillManager {
        if (this.INSTANCE) {
            return this.INSTANCE;
        }
        this.INSTANCE = new PlayerSkillManager();
        return this.INSTANCE;
    }

    private constructor() {
        // 初始化
        this.init();
    }

    /**
     * 初始化
     */
    private init() {
        this.player = PlayerDataManager.getInstance().getPlayer();
    }

    /**
     * 玩家是否已解锁所有技能
     */
    public isUnlockAllSkill(): boolean {
        let skills = CSVManager.getInstance().getTable(CSVPaths.playerSkill);
        // 为什么要-1？因为生命回复技能不在技能列表里面出现
        return this.player.skillIds.length === skills.datas.length - 1;
    }

    /**
     * 获取玩家未解锁的技能ID
     */
    public getLockPlayerSkill(): string[] {
        // 获取所有技能
        let skillIds = CSVManager.getInstance().getColumnDatas(CSVPaths.playerSkill, "ID");
        // 所有未解锁的技能
        return skillIds.filter((skillId) => {
            return this.player.skillIds.indexOf(skillId) === -1;
        });
    }

    /**
     * 添加技能
     *
     * @param skillId 技能ID
     */
    public addPlayerSkill(skillId: string) {
        // 当玩家没有这个技能时
        if (this.player.skillIds.indexOf(skillId) === -1) {
            this.player.skillIds.push(skillId);
            PlayerDataManager.getInstance().persistPlayer();
            EventManager.dispatchEvent(EventType.PARSE_PLAYER_SKILL);
        }
    }

    /**
     * 删除技能
     *
     * @param skillId 技能ID
     */
    public deletePlayerSkill(skillId: string) {
        let index = this.player.skillIds.indexOf(skillId);
        if (index !== -1) {
            this.player.skillIds.splice(index, 1);
            PlayerDataManager.getInstance().persistPlayer();
            EventManager.dispatchEvent(EventType.PARSE_PLAYER_SKILL)
        }
    }
}